<template>
  <el-container class="home-container">
    <!-- 头部区 -->
    <el-header>
      <div>
        <img src="../assets/head.jpg" alt />
        <span>商品后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollpase ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 给菜单el-menu开启路由模式 加router 二级菜单通过：index="路径完成路由跳转"-->
        <el-menu
         background-color="#333744"
         text-color="#fff"
         router 
         :default-active="activePath"
         :collapse="isCollpase" 
         :collapse-transition="false" 
         :unique-opened="true" 
         active-text-color="#409BFF">
          <!-- 一级菜单栏 
          :unique-opened="true"只打开一个菜单栏 
          前面加：绑定才会变成布尔值，不然是字符串
          只写unique-opened默认为true
          -->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
           
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconlist[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 路由跳转不加斜线不会跳转-->
            <el-menu-item 
            :index="'/'+subItem.path+''" 
            v-for="subItem in item.children" 
            @click="saveNavState('/'+subItem.path+'')"
            :key="subItem.id">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  
  data() {
    //左侧菜单数据
   return{ 
     menuList: [],
     iconlist:{
       '125':'iconfont icon-user',
       '103':'iconfont icon-lifangti',
       '101':'iconfont icon-shangpin ',
       '102':'iconfont icon-icon- ',
       '145':'iconfont icon-shujutongji'
     },
     //是否折叠
     isCollpase: false,
     //选择菜单效果
     activePath: ''
     }
  },
 created() {//生命周期函数
   this.getMenuList()
   this.activePath = window.sessionStorage.getItem('activePath')
 },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取所有菜单
    async getMenuList() {
      const {data: res} = await this.$http.get('menus')//解构data赋值给res
      if(res.meta.status !==200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
     console.log(this.menuList)
    },
    toggleCollapse() {
      this.isCollpase = !this.isCollpase
    },
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
    }
  },
};
</script>
<style lang='less' scoped>
.home-container {
  height: 100%;
}
.el-header {
  background: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 5px;
  align-items: center;
  font-size: 20px;
  color: white;
  div {
    display: flex;
    align-items: center;
  }
  div > img {
    width: 50px;
    height: 50px;
    margin-right: 10px;
    border-radius: 50%;
  }
}

.el-aside {
  background: hsl(226, 14%, 23%);
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button{
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
/deep/ .el-menu--vertical{
  position: relative;
  top:60px;
}
</style>